package com.example.wm.SanWar.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.SanWar.entity.Allies;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.SanWar.service.AlliesRecodeSheetService;
import com.example.wm.SanWar.service.AlliesService;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/AliesController")
public class AliesController {

    @Resource
    private AlliesService alliesService;
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private AlliesRecodeSheetService alliesRecodeSheetService;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody Allies allies) {

        allies.setCreatetime(tsyUtils.getnowtime());
        boolean b=    alliesService.save(allies);
        if(b){
            Integer id= allies.getId();

            alliesRecodeSheetService.insertList(allies.getCreatename(),"创建了同盟");

            return ResultData.success(id);

        }else{
            return ResultData.success("失败");

        }
    }
    @PostMapping("/alliesname")
    public Result alliesname(@RequestBody Allies allies) {
        LambdaQueryWrapper<Allies> lq=new LambdaQueryWrapper<>();
        lq.eq(Allies::getStatus,"正常");
        lq.eq(Allies::getCreatename,allies.getCreatename());
        lq.isNotNull(Allies::getTmname);
        List<Allies> list =alliesService.list(lq);
//        Set s=new HashSet<>();
//        for(Allies e:list){
//
//            s.add({"name":e.getTmname()});
//        }

        return ResultData.success(list);
    }

    @PostMapping("/findbyids")
    public Result findbyids(@RequestBody Allies allies) {
        LambdaQueryWrapper<Allies> lq=new LambdaQueryWrapper<>();
        lq.eq(Allies::getStatus,"正常");
        lq.eq(Allies::getId,allies.getId());
       Allies a =alliesService.getOne(lq);
        return ResultData.success(a);
    }


    @Transactional
    @PostMapping("/search")
    public Result search(@RequestBody Allies allies) {
        LambdaQueryWrapper<Allies> lq=new LambdaQueryWrapper<>();
        lq.like(Allies::getTmname,allies.getTmname());
        LambdaQueryWrapper<Allies> lq2=new LambdaQueryWrapper<>();
        lq2.like(Allies::getTmnumber,allies.getTmnumber());
     List<Allies> L= alliesService.list(lq);
     List<Allies> L2= alliesService.list(lq2);
        ArrayList<Allies> arr=new ArrayList<>();
        for (Allies a1:L){
            arr.add(a1);
        }
        for (Allies a2:L2){
            arr.add(a2);
        }
            return ResultData.success(arr);
    }

    @Transactional
    @PostMapping("/searchbyAllieaid")
    public Result searchbyAllieaid(@RequestBody Allies allies) {
        LambdaQueryWrapper<Allies> lq=new LambdaQueryWrapper<>();
        lq.eq(Allies::getTmnumber,allies.getTmnumber());
        List<Allies> L= alliesService.list(lq);
        return ResultData.success(L);

    }

    @Transactional
    @PostMapping("/update")
    public Result update(@RequestBody Allies allies) {
   Boolean b=   alliesService.updateById(allies);

        return ResultData.success(b);

    }


}
